<?php
namespace common\behaviors;

use Yii;
use yii\base\Behavior;
use common\component\exception\ErrMap;
use common\models\OperationLog;
use common\models\File;
use common\component\tool\Tool;


class Files extends Behavior
{
    
    public function upload($post, $uploadFileName, $com_id, $app_id = 0)
    {
        // P($com_id, false);
        /*判断文件类型*/
        // P($_FILES['UploadForm']['type']['file']);
        if(isset($_FILES['UploadForm']['type']['file'])){
            $fileType = $_FILES['UploadForm']['type']['file'];
            #文件类型1-zip 2-doc 2-xls 4 pdf 5-mp3 6-mp4 是数据库规定好的
            if(strpos($fileType, 'zip') !== false){
                $fileType = 1;
            }else if(strpos($fileType, 'msword') !== false or strpos($fileType, 'wordprocessingml.document') !== false){
                $fileType = 2;
            }else if(strpos($fileType, 'vnd.ms-excel') !== false or strpos($fileType, 'spreadsheetml.sheet') !== false){
                $fileType = 3;
            }else if(strpos($fileType, 'pdf') !== false){
                $fileType = 4;
            }else if(strpos($fileType, 'mp3') !== false){
                $fileType = 5;
            }else if(strpos($fileType, 'mp4') !== false){
                $fileType = 6;
            }
        }
        /*不是需要的类型*/
        if(!is_numeric($fileType)){
            $backInfo = ErrMap::getError(10019);//返回信息
        }else{
            if($_FILES){//有图片上传
                $file = new File();
                if($fileName = $file->upload($post, $uploadFileName, $fileType, $com_id, $app_id)){
                    $backInfo = [0, $fileName];//返回信息
                }else{
                    if($file->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $file->getErrors();//异常信息
                    }
                }
            }
        }


        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    
}
